#ircontrol.py
#检测红外遥控器是否正常
#!/usr/bin/python

import pylirc, time
import RPi.GPIO as GPIO
import time

makerobo_Rpin = 17
makerobo_Gpin = 18
makerobo_Bpin = 27
makerobo_blocking = 0
p_R = 0
p_G = 0
p_B = 0

rgb_Lv = [100, 20, 90] # Light Level
rgb_color = [100, 100, 100]  #默认100时，占空比,100-100=0，是关闭灯

def makerobo_ledColorSet(color):
    global p_R, p_G, p_B
    #更改占空比
    p_R.ChangeDutyCycle(100 - color[0])     # color[0]为控制红灯的颜色
    p_G.ChangeDutyCycle(100 - color[1])     # color[1]为控制绿灯的颜色
    p_B.ChangeDutyCycle(100 - color[2])     # color[2]为控制蓝灯的颜色

def makerobo_setup():
    global p_R, p_G, p_B
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(makerobo_Rpin, GPIO.OUT)
    GPIO.setup(makerobo_Gpin, GPIO.OUT)
    GPIO.setup(makerobo_Bpin, GPIO.OUT)
    
    p_R = GPIO.PWM(makerobo_Rpin, 2000) # Set Frequece to 2KHz
    p_G = GPIO.PWM(makerobo_Gpin, 2000)
    p_B = GPIO.PWM(makerobo_Bpin, 2000)
    
    p_R.start(0)
    p_G.start(0)
    p_B.start(0)
    pylirc.init("pylirc", "/etc/lirc/conf", makerobo_blocking) #初始化lirc

def makerobo_map(x,in_min,in_max,out_min,out_max):
    return (x-in_min)*(out_max-out_min)/(in_max-in_min)+out_min
    
def RGB_control(config):
    global color
    if config == 'KEY_NUMERIC_1':
        rgb_color[0] = rgb_Lv[0]    #rgb_Lv[0]为=100,占空比为100-100=0，红灯灭
        #print ('Makerobo Red OFF')

    if config == 'KEY_NUMERIC_3':
        rgb_color[0] = rgb_Lv[1]    #rgb_Lv[1]为=20,占空比为100-20=80，红灯亮
        #print ('Makerobo Light Red')
'''
    if config == 'KEY_NUMERIC_2':
        rgb_color[0] = rgb_Lv[2]    #rgb_Lv[2]为=90,占空比为100-90=10，红灯暗
        print ('Makerobo Red')

    if config == 'KEY_NUMERIC_4':
        rgb_color[1] = rgb_Lv[0]    #rgb_Lv[0]为=100,占空比为100-100=0，绿灯灭
        print ('Makerobo Green OFF')

    if config == 'KEY_NUMERIC_6':
        rgb_color[1] = rgb_Lv[1]    #rgb_Lv[1]为=20,占空比为100-20=80，绿灯亮
        print ('Makerobo Light Green')

    if config == 'KEY_NUMERIC_5':
        rgb_color[1] = rgb_Lv[2]    #rgb_Lv[2]为=90,占空比为100-90=10，绿灯暗
        print ('Makerobo Green')

    if config == 'KEY_NUMERIC_7':
        rgb_color[2] = rgb_Lv[0]    #rgb_Lv[0]为=100,占空比为100-100=0，蓝灯灭
        print ('Makerobo Blue OFF')

    if config == 'KEY_NUMERIC_9':
        rgb_color[2] = rgb_Lv[1]    #rgb_Lv[1]为=20,占空比为100-20=80，蓝灯亮
        print ('Makerobo Light Blue')

    if config == 'KEY_NUMERIC_8':
        rgb_color[2] = rgb_Lv[2]    #rgb_Lv[2]为=90,占空比为100-90=10，蓝灯暗
        print ('Makerobo BLUE')
'''

def makerobo_loop():
    while True:
        s = pylirc.nextcode(1)
#如果初始化是ok的，您可以轮询lirc的命令。要读取队列中的任何命令，请调用pylirc.nextcode()
#如果队列中没有命令，或者列表中包含读取的命令，则returnvalue为'None'。
        while(s):
            for (code) in s:
                print ('Command: ', code["config"]) #For debug: Uncomment this
#               line to see the return value of buttons
                RGB_control(code["config"])
                makerobo_ledColorSet(rgb_color)
            if(not makerobo_blocking):
                s = pylirc.nextcode(1)
            else:
                s = []

def destroy():
    p_R.stop()
    p_G.stop()
    p_B.stop()
    GPIO.output(makerobo_Rpin, GPIO.LOW)    # Turn off all leds
    GPIO.output(makerobo_Gpin, GPIO.LOW)
    GPIO.output(makerobo_Bpin, GPIO.LOW)
    GPIO.cleanup()
    pylirc.exit()

'''
if __name__ == '__main__':
    try:
        makerobo_setup()
        makerobo_loop()
    except KeyboardInterrupt:
        destroy()
'''